﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class LocationContactListControl : RSBaseUserControl
    {
        int _companyid = 0;
        int _locationid = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void gvLocationContact_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int locationid = Convert.ToInt32(hfSelectedLocationId.Value);
                int contactid = Convert.ToInt32(gvLocationContact.DataKeys[e.RowIndex].Value);
                RemoveContactFromLocation(companyid, locationid, contactid);
                ucModalActionMessageControl.ShowModal("Contact removed from location successfully!", Request.RawUrl, true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        private void RemoveContactFromLocation(int companyid, int locationid, int contactid)
        {
            if (companyid > 0 && locationid > 0 && contactid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@locationid", SqlDbType.Int, 0, ParameterDirection.Input, locationid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@contactid", SqlDbType.Int, 0, ParameterDirection.Input, contactid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblContactLocation WHERE ContactId=@contactid AND  LocationId=@locationid AND CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                
            }
        }

        
        protected void gvContacts_SelectedIndexChanged(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int contactid = Convert.ToInt32(gvLocationContact.SelectedValue.ToString());
            ucModalContactDetailControl.ShowModal(userIdentity.RSCompanyId, contactid);
        }

        public void LoadContactData(int companyid, int locationid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            if (companyid > 0 && locationid > 0)
            {
                hfSelectedLocationId.Value = locationid.ToString();

                String sqlSELECT = "SELECT c.* FROM [rs_tblContact] c INNER JOIN [rs_tblContactLocation] cl ON c.ContactId=cl.ContactId WHERE cl.CompanyId=@CompanyID AND cl.LocationId=@LocationID ORDER BY c.LastName, c.FirstName";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@locationid";
                param.Value = locationid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvLocationContact.DataSource = ds;
                gvLocationContact.DataBind();
                myConnection.Close();

                if (gvLocationContact.Rows.Count == 0)
                {
                    //divContactInfo.Visible = false;
                }
            }
        }

        protected void btnAddContactToLocation_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            
            if (userIdentity.RSCanWrite)
            {
                _companyid = userIdentity.RSCompanyId;
                if (!String.IsNullOrEmpty(hfSelectedLocationId.Value))
                    _locationid = Convert.ToInt32(hfSelectedLocationId.Value);
                if (_companyid > 0 && _locationid > 0)
                {
                    Response.Redirect("~/RS/SBM/RSManageContact.aspx?action=add&locationid=" + _locationid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

    }
}